package com.iweb.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {
    private final static String EXCHANGE_NAME = "topic.exchange";
    private final static String EXCHANGE_TYPE = "topic";
    private final static String QUEUE_A = "topic.queue.a";
    private final static String QUEUE_B = "topic.queue.b";

    public static void main(String[] args) throws Exception {
// 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("122.112.160.198");
        factory.setUsername("guest");
        factory.setPassword("guest");

        // 获取会话
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 声明队列
        channel.queueDeclare(QUEUE_A, false, false, false, null);
        channel.queueDeclare(QUEUE_B, false, false, false, null);
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);
        // 绑定队列到交换机
        channel.queueBind(QUEUE_A, EXCHANGE_NAME, "user.*"); // user.name 会进入 QUEUE_A，QUEUE_B
        channel.queueBind(QUEUE_B, EXCHANGE_NAME, "user.#"); // user.name.age 只会进入 QUEUE_B

        channel.basicPublish(EXCHANGE_NAME, "user.name", null, "jack".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "user.name.age", null, "jack-20".getBytes());

        channel.close();
        connection.close();
    }
}
